package com.DosonLeung.homeschoolcommunication.Utils;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Message;
import android.os.Handler;
import android.widget.Toast;

import com.DosonLeung.homeschoolcommunication.Constant.SystemConstant;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by DosonLeung on 2014/10/23.
 */
public class RecordUtil {
    public static String voicePath = "";
    //最长录音时间300s = 5min
    private static int voiceLengthMax = 300;
    //计时器
    private static Timer voiceRecordTimer;
    //计时任务
    private static TimerTask voiceRecordTimerTask;
    //反馈录音时间长度
    private static int voiceLengthRecorded = 0;
    //录音器
    private static MediaRecorder voiceRecorder;

    public static synchronized void startRecording(Activity app,final Handler handler,String target){
        String url = "";
        voicePath = "";
        if (!SystemConstant.voiceIsRecording) {
            voiceRecorder = new MediaRecorder();
            try {
                String fileName = "/"+target+TimeUtil.getTime()+ ".acc";
                voiceRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                voiceRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                voiceRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
                url = StorageUtil.saveMediaRecorderInVoice(voiceRecorder, fileName);
                voiceRecorder.prepare();
                voiceRecorder.start();
                SystemConstant.voiceIsRecording = true;
                voiceRecordTimer = new Timer();
                voiceRecordTimerTask = new TimerTask() {
                    @Override
                    public void run() {
                        Message msg = new Message();
                        if (voiceLengthMax > 0) {
                            voiceLengthMax--;
                            msg.what = 1003;
                        } else {
                            msg.what = 1004;
                        }
                        handler.sendMessage(msg);
                    }
                };
                voiceRecordTimer.schedule(voiceRecordTimerTask, 0, 1000);
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(app.getBaseContext(), "录音失败，请检查你的手机", Toast.LENGTH_SHORT).show();
                url = "";
            }
        }
        voicePath = url;
    }

    public static synchronized String stopRecording() {
        if (SystemConstant.voiceIsRecording) {
            voiceRecorder.stop();
            voiceRecorder.release();
            voiceRecorder = null;
            SystemConstant.voiceIsRecording = false;
            voiceRecordTimer.cancel();
            voiceRecordTimerTask.cancel();
            voiceLengthRecorded = 300 - voiceLengthMax;
            voiceLengthMax = 300;
        }
        return (Integer.valueOf(voiceLengthRecorded)).toString();
    }

    //播放录音
    public static synchronized void playVoice(MediaPlayer voicePlayer,String path) {
        if (SystemConstant.voiceIsPlaying) {
            voicePlayer.release();
        }
        voicePlayer = new MediaPlayer();
        try {
            voicePlayer.setDataSource(path);
            voicePlayer.prepare();
            voicePlayer.start();
            SystemConstant.voiceIsPlaying = true;
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    SystemConstant.voiceIsPlaying = false;
                }
            }, voicePlayer.getDuration());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int getMediaDuration(MediaPlayer voicePlayer,String path){
        voicePlayer = new MediaPlayer();
        try {
            File file = new File(path);
            if(file.exists()){
                voicePlayer.setDataSource(path);
                voicePlayer.prepare();
                return voicePlayer.getDuration();
            }else{
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
